<?php
/**
 * 使用示例
 * @author M2
 * @version 1.0
 * create Date 2013-11-5
 * update Date 2013-11-6
 * update Date 2013-11-7
 * <yl:channels channelname="首页"><yl:channel type="cname"/></yl:channels>
 * <yl:channels upLevel="1">
  	<yl:a href="http://www.baidu.com">Test</yl:a><br/>
  	<yl:channel type="cname"></yl:channel>(<yl:channel type="CountOfContents"></yl:channel>)<br>
   </yl:channels>
 */
class YlChannelsTaglib {
	public function __construct() {
		C(include SITE_PATH.'/extends/taglib/tagarr.php');
	}
	public function parse($content,$sid,$cateid, $domain,$channel) {
        if (stripos($content, 'yl:channels') !== false) {
            $matches = preg_parse($content, 'yl:channels');
            foreach ($matches as $match) {
                $parseStr = '';
                $where = 'siteid ='.$sid;
                $channelname = getParseOneAttr($match, 'channelname');
                $uplevel = getParseOneAttr($match, 'upLevel');
                $scope = getParseOneAttr($match, 'scope');
                $isimage = getParseOneAttr($match, 'isImage');
                $channlesText = getParseContentText($match, 'yl:channels');
                if (!empty($channelname)) { // 查询栏目下的子栏目
                    $cateid = M('cms_category')->where('cname="'.$channelname.'"')->getField('id');
                } elseif (isset($uplevel)) {
                    switch ($uplevel) {
                        case '0':
                            $cateid = $cateid;
                            break;
                        case '1':
                            $cateid = M('cms_category')->where('id='.$cateid)->getField('pid');
                            break;
                    }
                }

                if ($scope) {
                    if (strtolower($scope) === 'all') {
                        $gcate = D('Category')->getChildCate($cateid);
                        $where  .= ' and FIND_IN_SET( pid, "'.$gcate.'" )';
                    }
                } else {
                    $where .= ' and pid='.$cateid;
                }

                if (getParseOneAttr($match, 'where')) {
                    $where .= ' and '.getParseOneAttr($match, 'where');
                }
                if (isset($isimage) && !empty($isimage)) {
                    if ($isimage === 'true') {
                        $where .= ' and imageurl <> ""';
                    }
                }
                $query = M('cms_category')
                    ->field('(case when url <> "" then url when createurl <> "" then createurl
                            else (concat("'.$domain.'","/",replace(replace((case when catenaming <> ""
                		 then catenaming else ("'.$channel['catenaming'].'") end),"{cateid}",id),"{@ChannelID}",id))) end) as url,
                            replace(imageurl,"@","'.$domain.'") as imageurl,id,cname,content,keywords,description,pid')
                    ->where($where)
                    ->select();
                unset($where);
                if(!empty($query)) {
                    foreach($query as $k => $v) {
                    	//update by jepin  2014.10.10  缓存冲突
                        ///S('cache_channel_'.$v['id'], $v);

                        //update next 2013-12-12 简化替换操作
                        $text = taglib(C('yl:if'))->parse($channlesText, $v);
                        $text = taglib(C('yl:contents'))->parse($text,$sid,$v['id'],$domain,$channel);
                        $text = taglib(C('yl:a'))->parse($text,$sid,$v,'2',$domain,$channel);
                        $text = taglib(C('yl:channel'))->parse($text,$v,$sid,$domain, $channel);
                        $text = taglib(C('yl:image'))->parse($text,$v);
                        $text = taglib(C('yl:count'))->parse($text,$v);

                        if(strpos($text,'{') && strpos($text,'}')) {
                            preg_match_all('/\{Channel.(.*?)\}/eis',$text,$out);
                            for ($i=0; $i< count($out[1]); $i++) {
                                $text = str_ireplace($out[0][$i],$v[$out[1][$i]],$text);
                            }
                        }
                        $parseStr .= $text;
                    }
                } else {
                    $parseStr = '';
                }
                $content = str_replace($match, $parseStr, $content);
            }
        }
        return $content;
	}
}